﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace HML.Demo
{
    public partial class PieceProgressForm : ItemFormTemplate
    {
        public PieceProgressForm()
        {
            InitializeComponent();
            this.Load += new System.EventHandler(this.Form_Load);
            this.Shown += new System.EventHandler(this.Form_Shown);
        }

        #region 实现

        public override ItemFormTemplateViewObject AsynchRequestTask(ItemFormTemplateViewObject view)
        {
            System.Threading.Thread.Sleep(300);
            view.ViewData = DemoHelper.GetDescription(AppDomain.CurrentDomain.BaseDirectory + "例子\\控件\\PieceProgressForm\\PieceProgressForm.rtf");
            return view;
        }

        public override void DataBind(ItemFormTemplateViewObject viewObject)
        {
            this.introduceBrowser.Rtf = viewObject.ViewData.ToString();
        }

        public override void ShowView()
        {
            base.ShowView();
        }

        public override void HideView()
        {
            base.HideView();
        }

        public override void CloseView()
        {
            base.CloseView();
        }

        #endregion

        #region 属性面板

        private int[] errorArr = new int[] { 3, 23, 16, 20, 34, 35, 38, 50, 57, 59, 65, 68, 69, 70 };
        private int[] successArr = new int[] { 1, 2, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15 };
        private int[] handleArr = new int[] { 71, 72, 73, 74, 75 };

        private void Form_Load(object sender, EventArgs e)
        {
            this.controlContainer.SizeChanged += this.ControlContainer_SizeChanged;

            this.demo_control.PieceSize = 8;
            this.demo_control.PieceAmount = 100;
            this.demo_control.SetAllPieceStatus(PieceProgressStatus.Normal);
            this.demo_control.SetPieceStatus(this.errorArr, PieceProgressStatus.Error);
            this.demo_control.SetPieceStatus(this.successArr, PieceProgressStatus.Success);
            this.demo_control.SetPieceStatus(this.handleArr, PieceProgressStatus.Executing);

            this.pieceSize_label.Text = this.demo_control.PieceSize.ToString();
            this.pieceSize_trackBarPlus.Value = this.demo_control.PieceSize;
            this.pieceSize_trackBarPlus.ValueChanged += this.PieceSize_trackBarPlus_ValueChanged;

            this.pieceCount_label.Text = this.demo_control.PieceAmount.ToString();
            this.pieceCount_trackBarPlus.Value = this.demo_control.PieceAmount;
            this.pieceCount_trackBarPlus.ValueChanged += this.PieceAmount_trackBarPlus_ValueChanged;

            this.error_textBox.Text = String.Join("," ,this.errorArr);
            this.error_textBox.LostFocus += this.Error_textBox_LostFocus;

            this.success_textBox.Text = String.Join(",", this.successArr);
            this.success_textBox.LostFocus += this.Success_textBox_LostFocus;

            this.handle_textBox.Text = String.Join(",", this.handleArr);
            this.handle_textBox.LostFocus += this.Handle_textBox_LostFocus;
        }

        private void Form_Shown(object sender, EventArgs e)
        {
            this.splitContainer.SplitterDistance = Math.Max(this.splitContainer.Width / 4, this.splitContainer.Width - MainForm.GlobalSplitWidth);
            this.splitContainer.SplitterMoved += this.SplitContainer_SplitterMoved;

            MainForm.LoadAssistTabControl(this.assistTabControl);
        }

        private void SplitContainer_SplitterMoved(object sender, SplitterEventArgs e)
        {
            SplitContainer control = (SplitContainer)sender;
            MainForm.GlobalSplitWidth = this.splitContainer.Width - control.SplitterDistance;
        }

        private void ControlContainer_SizeChanged(object sender, EventArgs e)
        {
            Panel control = (Panel)sender;
            this.demo_control.Location = new Point((control.Width - this.demo_control.Width) / 2, (control.Height - this.demo_control.Height) / 2);
        }

        private void PieceSize_trackBarPlus_ValueChanged(object sender, EventArgs e)
        {
            TrackBarPlus control = (TrackBarPlus)sender;
            this.demo_control.PieceSize = (int)control.Value;
            this.pieceSize_label.Text = this.demo_control.PieceSize.ToString();
        }

        private void PieceAmount_trackBarPlus_ValueChanged(object sender, EventArgs e)
        {
            TrackBarPlus control = (TrackBarPlus)sender;
            this.demo_control.PieceAmount = (int)control.Value;
            this.pieceCount_label.Text = this.demo_control.PieceAmount.ToString();
        }

        private void Error_textBox_LostFocus(object sender, EventArgs e)
        {
            TextBox control = (TextBox)sender;
            string[] arr = control.Text.Trim().Split(new char[] { ',' });
            int[] indexArr = new int[arr.Length];
            for (int i = 0; i < arr.Length; i++)
            {
                indexArr[i] = int.Parse(arr[i]);
            }
            this.demo_control.SetPieceStatus(indexArr, PieceProgressStatus.Error);
        }

        private void Success_textBox_LostFocus(object sender, EventArgs e)
        {
            TextBox control = (TextBox)sender;
            string[] arr = control.Text.Trim().Split(new char[] { ',' });
            int[] indexArr = new int[arr.Length];
            for (int i = 0; i < arr.Length; i++)
            {
                indexArr[i] = int.Parse(arr[i]);
            }
            this.demo_control.SetPieceStatus(indexArr, PieceProgressStatus.Success);
        }

        private void Handle_textBox_LostFocus(object sender, EventArgs e)
        {
            TextBox control = (TextBox)sender;
            string[] arr = control.Text.Trim().Split(new char[] { ',' });
            int[] indexArr = new int[arr.Length];
            for (int i = 0; i < arr.Length; i++)
            {
                indexArr[i] = int.Parse(arr[i]);
            }
            this.demo_control.SetPieceStatus(indexArr, PieceProgressStatus.Executing);
        }

        #endregion

    }
}
